if {"::tcltest" ni [namespace children]} {
	package require tcltest
	namespace import ::tcltest::*
}

package require rl_json

source [file join [file dirname [info script]] helpers.tcl]

test new-1.1 {Create an array from type-value pairs} -setup { #<<<
	set typevalues [lmap v {a 1 c 2 e 3 g} {
		if {[string is digit $v]} {
			list number $v
		} else {
			list string $v
		}
	}]
} -body {
	json new array {*}$typevalues
} -cleanup {
	unset -nocomplain typevalues
} -result {["a",1,"c",2,"e",3,"g"]}
#>>>
test new-1.1.1 {Create an array from type-value pairs, syntax error in elem (too many args)} -setup { #<<<
	set typevalues [lmap v {a 1 c 2 e 3 g} {
		if {[string is digit $v]} {
			list number $v x
		} else {
			list string $v
		}
	}]
} -body {
	json new array {*}$typevalues
} -cleanup {
	unset -nocomplain typevalues
} -returnCodes error -result {wrong # args: should be "number value"}
#>>>

test new-1.2 {Create an array from JSON values} -setup { #<<<
	set values [lmap v {a 1 c 2 e 3 g} {
		if {[string is digit $v]} {
			json new number $v;
		} else {
			json new string $v;
		}
	}];
} -body {
	json new array {*}[lmap v $values {
		list json $v;
	}];
} -cleanup {
	unset -nocomplain values v;
} -result {["a",1,"c",2,"e",3,"g"]}
#>>>
test new-2.1 {json string, plain string} -body { #<<<
	json string new-2.1
} -result {"new-2.1"}
#>>>
test new-2.2 {json string, too few args} -body { #<<<
	set code [catch {json string} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {wrong # args: should be "*string value"} {TCL WRONGARGS}] -match glob
#>>>
test new-2.3 {json string, too many args} -body { #<<<
	set code [catch {json string foo bar} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {wrong # args: should be "*string value"} {TCL WRONGARGS}] -match glob
#>>>
test new-2.4 {json string, template string} -body { #<<<
	json string ~S:foo
} -result {"~S:foo"}
#>>>
test new-3.1 {json number, int} -body { #<<<
	json number 1234
} -result 1234
#>>>
test new-3.6 {json number, float} -body { #<<<
	json number 1234.4
} -result 1234.4
#>>>
test new-4.1.1 {json boolean, truthy int} -body { #<<<
	json boolean 1234
} -result true
#>>>
test new-4.1.2 {json boolean, truthy string: true} -body { #<<<
	json boolean true
} -result true
#>>>
test new-4.1.3 {json boolean, truthy string: tr} -body { #<<<
	json boolean tr
} -result true
#>>>
test new-4.1.4 {json boolean, truthy string: yes} -body { #<<<
	json boolean yes
} -result true
#>>>
test new-4.1.5 {json boolean, truthy string: on} -body { #<<<
	json boolean on
} -result true
#>>>
test new-4.1.6 {json boolean, truthy int: -1} -body { #<<<
	json boolean -1
} -result true
#>>>
test new-4.1.7 {json boolean, truthy int} -body { #<<<
	json boolean 0
} -result false
#>>>
test new-4.1.8 {json boolean, truthy string: false} -body { #<<<
	json boolean false
} -result false
#>>>
test new-4.1.9 {json boolean, truthy string: fal} -body { #<<<
	json boolean fal
} -result false
#>>>
test new-4.1.10 {json boolean, truthy string: no} -body { #<<<
	json boolean no
} -result false
#>>>
test new-4.1.11 {json boolean, truthy string: off} -body { #<<<
	json boolean off
} -result false
#>>>
test new-4.2 {json boolean, too few args} -body { #<<<
	set code [catch {json boolean} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {wrong # args: should be "*boolean value"} {TCL WRONGARGS}] -match glob
#>>>
test new-4.3 {json boolean, too many args} -body { #<<<
	set code [catch {json boolean foo bar} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {wrong # args: should be "*boolean value"} {TCL WRONGARGS}] -match glob
#>>>
test new-4.4 {json boolean, invalid string} -body { #<<<
	set code [catch {json boolean foo} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {expected boolean value but got "foo"} {TCL VALUE NUMBER}]
#>>>
test new-4.5 {json boolean, empty string} -body { #<<<
	set code [catch {json boolean ""} r o]
	list $code $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain code r o
} -result [list 1 {expected boolean value but got ""} {TCL VALUE NUMBER}]
#>>>
test new-4.6 {json boolean, float} -body { #<<<
	json boolean 1234.4
} -result true
#>>>
test new-4.7 {json boolean, scientific notation} -body { #<<<
	json boolean 1e6
} -result true
#>>>
test new-5.1.1 {Create an object - args} -body { #<<<
	json new object a {number 1} c {num 2} e {num 3} g {num 4}
} -result {{"a":1,"c":2,"e":3,"g":4}}
#>>>
test new-5.1.2 {Create an object - single arg} -body { #<<<
	json new object {a {number 1} c {num 2} e {num 3} g {num 4}}
} -result {{"a":1,"c":2,"e":3,"g":4}}
#>>>
test new-6.1 {Create a string} -body { #<<<
	json new string "hello, world"
} -result {"hello, world"}
#>>>
test new-6.2 {Create a template: string} -body { #<<<
	json new string "~S:foo"
} -result {"~S:foo"}
#>>>
test new-6.3 {Create a template: number} -body { #<<<
	json new string "~N:foo"
} -result {"~N:foo"}
#>>>
test new-6.4 {Create a template: boolean} -body { #<<<
	json new string "~B:foo"
} -result {"~B:foo"}
#>>>
test new-6.5 {Create a template: json} -body { #<<<
	json new string "~J:foo"
} -result {"~J:foo"}
#>>>
test new-6.6 {Create a template: template} -body { #<<<
	json new string "~T:foo"
} -result {"~T:foo"}
#>>>
test new-6.7 {Create a template: literal} -body { #<<<
	json new string "~L:~S:foo"
} -result {"~L:~S:foo"}
#>>>
test new-6.7 {Create a template: undef} -body { #<<<
	json new string "~X:foo"
} -result {"~X:foo"}
#>>>
test new-6.8 {Create new bool: true} -body { #<<<
	json new true
} -result true
#>>>
test new-6.9 {Create new bool: false} -body { #<<<
	json new false
} -result false
#>>>
test new-6.10.1 {Create new bool: value} -body { #<<<
	json new bool
} -returnCodes error -result {Wrong # of arguments.  Must be "boolean val"}
#>>>
test new-6.10.2 {Create new bool: value} -body { #<<<
	json new bool true false
} -returnCodes error -result {Wrong # of arguments.  Must be "boolean val"}
#>>>
test new-6.10.3 {Create new bool: value} -body { #<<<
	json new bool true
} -result true
#>>>
test new-6.10.4 {Create new bool: value} -body { #<<<
	json new bool tr
} -result true
#>>>
test new-6.10.5 {Create new bool: value} -body { #<<<
	json new bool false
} -result false
#>>>
test new-6.10.6 {Create new bool: value} -body { #<<<
	json new bool no
} -result false
#>>>
test new-6.10.7 {Create new bool: value} -body { #<<<
	json new bool 0
} -result false
#>>>
test new-6.10.8 {Create new bool: value} -body { #<<<
	json new bool -42
} -result true
#>>>
test new-6.11 {Create new null} -body { #<<<
	json new null
} -result null
#>>>

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# tab-width: 4
# End:
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
